package com.cloudsigns.factorymanager2.project.model;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForBaseInfoBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForSizeBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by tony on 2018/8/18.
 */

public class SurveyFeedbackAddModel implements ISurveyFeedbackAddModel {

    @Override
    public void surveyFeedbackAdd(final String orderId, final List<SurveyFeedBackForBaseInfoBean> baseInfoList, final List<SurveyFeedBackForBaseInfoBean> prepareList, final List<SurveyFeedBackForSizeBean> sizeList, final OnSurveyFeedbackAdListener surveyFeedbackAdListener) {
        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.SURVEY_FEEDBACK_ADD);

                HttpResponse response;
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("orderId", orderId);
                    if (null != baseInfoList) {
                        JSONArray baseInfoJsonArr = new JSONArray();
                        for (SurveyFeedBackForBaseInfoBean surveyFeedBackForBaseInfoBean : baseInfoList) {
                            JSONObject baseInfoJsonObj = new JSONObject();
                            baseInfoJsonObj.put("categoryDesc", surveyFeedBackForBaseInfoBean.getCategoryDesc());
                            baseInfoJsonObj.put("desc", surveyFeedBackForBaseInfoBean.getDesc());
                            baseInfoJsonArr.put(baseInfoJsonObj);
                        }
                        jsonObject.put("baseInfoList", baseInfoJsonArr);
                    }

                    if (null != prepareList) {
                        JSONArray prepareListJsonArr = new JSONArray();
                        for (SurveyFeedBackForBaseInfoBean surveyFeedBackForBaseInfoBean : prepareList) {
                            JSONObject  prepareJsonObj = new JSONObject();
                            prepareJsonObj.put("categoryDesc", surveyFeedBackForBaseInfoBean.getCategoryDesc());
                            prepareJsonObj.put("desc", surveyFeedBackForBaseInfoBean.getDesc());
                            prepareListJsonArr.put(prepareJsonObj);
                        }
                        jsonObject.put("prepareList", prepareListJsonArr);
                    }

                    if (null != prepareList) {
                        JSONArray sizeJsonArr = new JSONArray();
                        for (SurveyFeedBackForSizeBean surveyFeedBackForSizeBean : sizeList) {
                            JSONObject  sizeJsonObj = new JSONObject();
                            sizeJsonObj.put("arcHight", surveyFeedBackForSizeBean.getArcHight());
                            sizeJsonObj.put("arcLength", surveyFeedBackForSizeBean.getArcLength());
                            sizeJsonObj.put("content", surveyFeedBackForSizeBean.getContent());
                            sizeJsonObj.put("corner", surveyFeedBackForSizeBean.getCorner());
                            sizeJsonObj.put("hight", surveyFeedBackForSizeBean.getHight());
                            sizeJsonObj.put("hightDif", surveyFeedBackForSizeBean.getHightDif());
                            List<String> imgUrlList = surveyFeedBackForSizeBean.getImgUrlList();
                            if(null != imgUrlList){
                                JSONArray imgJsonArr = new JSONArray();
                                for(String url : imgUrlList){
                                    imgJsonArr.put(url);
                                }
                                sizeJsonObj.put("imgUrlList", imgJsonArr);
                            }

                            sizeJsonObj.put("specialHight", surveyFeedBackForSizeBean.getSpecialHight());
                            sizeJsonObj.put("specialWidth", surveyFeedBackForSizeBean.getSpecialWidth());
                            sizeJsonObj.put("width", surveyFeedBackForSizeBean.getWidth());

                            sizeJsonArr.put(sizeJsonObj);
                        }

                        jsonObject.put("sizeList", sizeJsonArr);
                    }


                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);

                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.setEntity(se);

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != surveyFeedbackAdListener) {

                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            surveyFeedbackAdListener.surveyFeedbackAdSuccessed();
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            surveyFeedbackAdListener.surveyFeedbackAdFailed(error_str);


                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnSurveyFeedbackAdListener {
        void surveyFeedbackAdSuccessed();

        void surveyFeedbackAdFailed(String desc);
    }
}
